/**
 * 
 */
package com.guoxianwei.scm.web.actions;

import java.io.StringReader;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.SessionScope;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.integration.spring.SpringBean;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.guoxianwei.scm.domain.Employee;
import com.guoxianwei.scm.service.EmployeeService;
import com.guoxianwei.scm.web.ajaxresult.AjaxResult;

/**
 * @author guoxianwei
 * 
 */
@SessionScope
public class EmployeeActionBean extends AbstractActionBean {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1767714708233127983L;

	private static final String MAIN = "/WEB-INF/jsp/employee/EmployeeList.jsp";

	private static final String EMPLOYEES_DETAIL = "/WEB-INF/jsp/employee/EmployeesDetail.jsp";

	private static final Log LOGGER = LogFactory
			.getLog(EmployeeActionBean.class);

	@SpringBean
	private transient EmployeeService employeeService;
	private Employee employee;

	private boolean isView;

	private String name;

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean getIsView() {
		return isView;
	}

	public void setIsView(boolean isView) {
		this.isView = isView;
	}

	@DefaultHandler
	public Resolution viewEmployee() {
		return new ForwardResolution(MAIN);
	}

	public Resolution removeEmployee() throws Exception {
		employeeService.removeEmployee(employee.getId());
		return new StreamingResolution("text", new StringReader(AjaxResult
				.success().toString()));
	}

	public Resolution getEmployees() throws Exception {
		employee = employeeService.getEmployeeById(employee.getId());
		return new StreamingResolution("text", new StringReader(AjaxResult
				.success(employee).toString()));
	}

	public Resolution newEmployeeForm() throws Exception {
		isView = false;
		employee = null;
		return new ForwardResolution(EMPLOYEES_DETAIL);
	}

	public Resolution newEmployee() throws Exception {
		employeeService.insertEmployee(employee);
		return new StreamingResolution("text", new StringReader(AjaxResult
				.success().toString()));
	}

	public Resolution editEmployeeForm() throws Exception {
		isView = false;
		employee = null;
		return new ForwardResolution(EMPLOYEES_DETAIL);
	}

	public Resolution viewEmployeeForm() throws Exception {
		isView = true;
		employee = null;
		return new ForwardResolution(EMPLOYEES_DETAIL);
	}
}
